﻿Imports System.Data
Imports MyGeneration.dOOdads
Imports System.Security.Cryptography
Imports System.Text

Public Class rc_Mantenimiento

    Private _MessageError As Exception = Nothing
    Public Property MessageError() As Exception
        Get
            Return _MessageError
        End Get
        Set(value As Exception)
            _MessageError = value
        End Set
    End Property


#Region "Usuarios"

    Private Function ValidateSHA1HashData(inputData As String, storedHashData As String) As Boolean
        'hash input text and save it string variable
        Dim getHashInputData As String = GetSHA1HashData(inputData)

        If String.Compare(getHashInputData, storedHashData) = 0 Then
            Return True
        Else
            Return False
        End If
    End Function

    ''' <summary>
    ''' take any string and encrypt it using SHA1 then
    ''' return the encrypted data
    ''' </summary>
    ''' <param name="data">input text you will enterd to encrypt it</param>
    ''' <returns>return the encrypted text as hexadecimal string</returns>
    Private Function GetSHA1HashData(data As String) As String
        'create new instance of md5
        Dim sha1__1 As SHA1 = SHA1.Create()

        'convert the input text to array of bytes
        Dim hashData As Byte() = sha1__1.ComputeHash(Encoding.[Default].GetBytes(data))

        'create new instance of StringBuilder to save hashed data
        Dim returnValue As New StringBuilder()

        'loop for each byte and add it to StringBuilder
        For i As Integer = 0 To hashData.Length - 1
            returnValue.Append(hashData(i).ToString())
        Next

        ' return hexadecimal string
        Return returnValue.ToString()
    End Function

    Public Function Insert_Log(conn As String, descripcion As String, codUsuario As Integer, codFuncion As Integer, Optional dt As DataTable = Nothing) As Integer

        Dim tx As TransactionMgr
        tx = TransactionMgr.ThreadTransactionMgr()

        Try
            tx.BeginTransaction()

            Dim datos As String = ""
            If Not dt Is Nothing Then
                For x As Integer = 0 To dt.Rows.Count - 1
                    For i As Integer = 0 To dt.Columns.Count - 1
                        datos += dt.Columns(i).ColumnName & ": " & dt.Rows(x)(i).ToString() & " | " '& Environment.NewLine
                    Next
                Next
            End If

            Dim usu As New Class_RC_Data.Class_RC.LOG(conn)

            usu.AddNew()
            usu.Descripcion = descripcion
            usu.Fecha = DateTime.Now
            usu.CodUsuario = codUsuario
            usu.CodFuncion = codFuncion
            usu.Datos = datos
            usu.Save()

            tx.CommitTransaction()

            Return usu.CodLog

        Catch ex As Exception
            tx.RollbackTransaction()
            MessageError = ex
            Return 0
        End Try

    End Function

    Public Function Insert_Usuario(conn As String, usuario As String, pass As String, nombre As String,
                                   apellido As String, ci As String, idArea As Integer) As Integer

        Dim tx As TransactionMgr
        tx = TransactionMgr.ThreadTransactionMgr()

        Try
            tx.BeginTransaction()
            'aca log
            Dim usu As New Class_RC_Data.Class_RC.USUARIOS(conn)

            usu.AddNew()
            usu.Usuario = usuario
            usu.Pass = GetSHA1HashData(pass)
            usu.Nombre = nombre
            usu.Apellido = apellido
            usu.Ci = ci
            usu.Area = idArea
            usu.Fechaalta = DateTime.Now
            usu.SetColumnNull("Fechabaja")
            usu.Active = True
            usu.Save()

            tx.CommitTransaction()

            Return usu.Codusuario

        Catch ex As Exception
            tx.RollbackTransaction()
            MessageError = ex
            Return 0
        End Try

    End Function

    Public Function Insert_Usuario_Rol(conn As String, codUsuario As Integer, codRol As Integer) As Integer

        Dim tx As TransactionMgr
        tx = TransactionMgr.ThreadTransactionMgr()

        Try
            tx.BeginTransaction()
            'aca log
            Dim usu As New Class_RC_Data.Class_RC.USUARIO_ROLE(conn)

            usu.AddNew()
            usu.Codusuario = codUsuario
            usu.CodRole = codRol
            usu.Save()

            tx.CommitTransaction()

            Return usu.Codusuario

        Catch ex As Exception
            tx.RollbackTransaction()
            MessageError = ex
            Return 0
        End Try

    End Function

    Public Function Insert_Rol_Funcion(conn As String, codRol As Integer, codFuncion As Integer) As Integer

        Dim tx As TransactionMgr
        tx = TransactionMgr.ThreadTransactionMgr()

        Try
            tx.BeginTransaction()
            'aca log
            Dim usu As New Class_RC_Data.Class_RC.FUNCIONES_ROLES(conn)

            usu.AddNew()
            usu.CodFuncion = codFuncion
            usu.CodRole = codRol
            usu.Save()

            tx.CommitTransaction()

            Return usu.CodFuncion

        Catch ex As Exception
            tx.RollbackTransaction()
            MessageError = ex
            Return 0
        End Try

    End Function

    Public Function Delete_Usuario_Rol(conn As String, codUsuario As Integer, codRol As Integer) As Boolean

        Dim tx As TransactionMgr
        tx = TransactionMgr.ThreadTransactionMgr()

        Try

            tx.BeginTransaction()

            Dim usu As New Class_RC_Data.Class_RC.USUARIO_ROLE(conn)
            usu.Where.Codusuario.Value = codUsuario
            usu.Where.CodRole.Value = codRol

            If usu.Query.Load Then
                usu.MarkAsDeleted()
                usu.Save()
            End If

            tx.CommitTransaction()

            Return True

        Catch ex As Exception
            tx.RollbackTransaction()
            MessageError = ex
            Return False
        End Try

    End Function

    Public Function Delete_Rol_Funcion(conn As String, codRol As Integer, codFuncion As Integer) As Boolean

        Dim tx As TransactionMgr
        tx = TransactionMgr.ThreadTransactionMgr()

        Try

            tx.BeginTransaction()

            Dim usu As New Class_RC_Data.Class_RC.FUNCIONES_ROLES(conn)
            usu.Where.CodRole.Value = codRol
            usu.Where.CodFuncion.Value = codFuncion

            If usu.Query.Load Then
                usu.MarkAsDeleted()
                usu.Save()
            End If

            tx.CommitTransaction()

            Return True

        Catch ex As Exception
            tx.RollbackTransaction()
            MessageError = ex
            Return False
        End Try

    End Function

    Public Function Update_Usuario(conn As String, usuario As String, pass As String, nombre As String,
                                   apellido As String, ci As String, idArea As Integer, codUsuario As Integer,
                                   activo As Boolean, codUsuarioUpdate As Integer) As Boolean

        Dim tx As TransactionMgr
        tx = TransactionMgr.ThreadTransactionMgr()

        Try
            tx.BeginTransaction()

            Dim usu As New Class_RC_Data.Class_RC.USUARIOS(conn)
            usu.Where.Codusuario.Value = codUsuario

            If usu.Query.Load Then

                'Registro anterior
                Insert_Log(conn, "Actualizar registro anterior usuario, codigo: " & codUsuario, codUsuarioUpdate, 42, usu.DefaultView.Table)

                usu.Usuario = usuario

                If pass <> "" Then
                    usu.Pass = GetSHA1HashData(pass)
                End If

                usu.Nombre = nombre
                usu.Apellido = apellido
                usu.Ci = ci
                usu.Area = idArea
                usu.Fechaalta = DateTime.Now
                usu.Active = activo

                If Not activo Then
                    usu.Fechabaja = DateTime.Now
                Else
                    usu.SetColumnNull("Fechabaja")
                End If

                usu.Save()

            End If

            'Registro editado
            Insert_Log(conn, "Actualizar registro usuario editado, codigo: " & codUsuario, codUsuarioUpdate, 42, usu.DefaultView.Table)

            tx.CommitTransaction()
            Return True

        Catch ex As Exception
            tx.RollbackTransaction()
            MessageError = ex
            Return False
        End Try

    End Function

    Public Function Delete_Usuario(conn As String, codUsuario As Integer) As Boolean

        Dim tx As TransactionMgr
        tx = TransactionMgr.ThreadTransactionMgr()

        Try
            tx.BeginTransaction()

            Dim usu As New Class_RC_Data.Class_RC.USUARIOS(conn)
            usu.Where.Codusuario.Value = codUsuario

            If usu.Query.Load Then

                usu.MarkAsDeleted()
                usu.Save()

            End If

            tx.CommitTransaction()
            Return True

        Catch ex As Exception
            tx.RollbackTransaction()
            MessageError = ex
            Return False
        End Try

    End Function

    Public Function controlAccesosPermisos(conn As String, codUsuario As Integer, descripFuncion As String) As Boolean
        Try
            Dim usua As New Class_RC_Data.Class_RC.VistaUsuarios_Rol_Funcion(conn)
            usua.Where.Codusuario.Value = codUsuario
            usua.Where.DescripcionFuncion.Value = descripFuncion
            If usua.Query.Load() Then
                Return True
            Else
                Return False
            End If
        Catch ex As Exception
            MessageError = ex
            Return False
        End Try
    End Function

    Public Function Login(conn As String, user As String, pass As String) As Integer

        Dim usuario As New Class_RC_Data.Class_RC.USUARIOS(conn)
        usuario.Where.Usuario.Value = user
        usuario.Where.Pass.Value = GetSHA1HashData(pass)
        If usuario.Query.Load Then
            If usuario.Active Then
                Return usuario.Codusuario

            Else
                Return -1

            End If

        Else
            Return 0

        End If

    End Function

    Public Function get_roles_usuario(conn As String, codUsuario As Integer, poseeRol As Integer) As DataTable
        Try

            Dim vTipo As New Class_RC_Data.Class_RC.USUARIOS(conn)
            Dim ds As DataSet

            ds = vTipo.proc_roles_usuarios(conn, codUsuario, poseeRol)

            If Not ds Is Nothing Then
                If ds.Tables.Count > 0 Then
                    If ds.Tables(0).Rows.Count > 0 Then
                        Return ds.Tables(0)
                    Else
                        Return Nothing
                    End If
                Else
                    Return Nothing
                End If
            Else
                Return Nothing
            End If

        Catch ex As Exception
            MessageError = ex
            Return Nothing
        End Try

    End Function

    Public Function get_funciones_rol(conn As String, codRol As Integer, poseeFuncion As Integer) As DataTable
        Try

            Dim vTipo As New Class_RC_Data.Class_RC.USUARIOS(conn)
            Dim ds As DataSet

            ds = vTipo.proc_funciones_roles(conn, codRol, poseeFuncion)

            If Not ds Is Nothing Then
                If ds.Tables.Count > 0 Then
                    If ds.Tables(0).Rows.Count > 0 Then
                        Return ds.Tables(0)
                    Else
                        Return Nothing
                    End If
                Else
                    Return Nothing
                End If
            Else
                Return Nothing
            End If

        Catch ex As Exception
            MessageError = ex
            Return Nothing
        End Try

    End Function

    Public Function get_Usuario_All(conn As String, codUsuario As Integer, usuario As String, nombre As String, apellido As String, idArea As Integer) As DataTable
        Try

            Dim usu As New Class_RC_Data.Class_RC.VistaUsuarios(conn)

            If codUsuario > 0 Then
                usu.Where.Codusuario.Value = codUsuario
            End If

            If idArea > 0 Then
                usu.Where.Area.Value = idArea
            End If

            If usuario <> "" Then
                usu.Where.Usuario.Value = "%" & usuario & "%"
                usu.Where.Usuario.Operator = WhereParameter.Operand.Like
            End If

            If nombre <> "" Then
                usu.Where.Nombre.Value = "%" & nombre & "%"
                usu.Where.Nombre.Operator = WhereParameter.Operand.Like
            End If

            If apellido <> "" Then
                usu.Where.Apellido.Value = "%" & apellido & "%"
                usu.Where.Apellido.Operator = WhereParameter.Operand.Like
            End If

            usu.Query.AddOrderBy("usuario", WhereParameter.Dir.ASC)

            If usu.Query.Load Then
                If usu.DefaultView.Table.Rows.Count > 0 Then
                    Return usu.DefaultView.Table
                Else
                    Return Nothing
                End If

            Else
                Return Nothing

            End If

        Catch ex As Exception
            MessageError = ex
            Return Nothing
        End Try

    End Function

    Public Function get_Log_All(conn As String, descripcion As String, codUsuario As Integer, codFuncion As Integer, fechaInicio As DateTime, fechaFin As DateTime) As DataTable
        Try

            Dim usu As New Class_RC_Data.Class_RC.VistaLog(conn)

            If codUsuario > 0 Then
                usu.Where.CodUsuario.Value = codUsuario
            End If

            If codFuncion > 0 Then
                usu.Where.CodFuncion.Value = codFuncion
            End If

            If descripcion <> "" Then
                usu.Where.Descripcion.Value = "%" & descripcion & "%"
                usu.Where.Descripcion.Operator = WhereParameter.Operand.Like
            End If

            usu.Where.Fecha.BetweenBeginValue = New DateTime(fechaInicio.Year, fechaInicio.Month, fechaInicio.Day, 0, 0, 0)
            usu.Where.Fecha.BetweenEndValue = New DateTime(fechaFin.Year, fechaFin.Month, fechaFin.Day, 23, 59, 59)
            usu.Where.Fecha.Operator = WhereParameter.Operand.Between

            usu.Query.AddOrderBy("codLog", WhereParameter.Dir.DESC)

            If usu.Query.Load Then
                If usu.DefaultView.Table.Rows.Count > 0 Then
                    Return usu.DefaultView.Table
                Else
                    Return Nothing
                End If

            Else
                Return Nothing

            End If

        Catch ex As Exception
            MessageError = ex
            Return Nothing
        End Try

    End Function

    Public Function get_Funciones_All(conn As String, descripcion As String) As DataTable
        Try

            Dim usu As New Class_RC_Data.Class_RC.FUNCIONES(conn)

            If descripcion <> "" Then
                usu.Where.DescripcionFuncion.Value = "%" & descripcion & "%"
                usu.Where.DescripcionFuncion.Operator = WhereParameter.Operand.Like
            End If

            usu.Query.AddOrderBy("DescripcionFuncion", WhereParameter.Dir.ASC)

            If usu.Query.Load Then
                If usu.DefaultView.Table.Rows.Count > 0 Then
                    Return usu.DefaultView.Table
                Else
                    Return Nothing
                End If

            Else
                Return Nothing

            End If

        Catch ex As Exception
            MessageError = ex
            Return Nothing
        End Try

    End Function

    Public Function get_Roles_All(conn As String, descripcion As String) As DataTable
        Try

            Dim usu As New Class_RC_Data.Class_RC.ROLES(conn)

            If descripcion <> "" Then
                usu.Where.DescripcionRole.Value = "%" & descripcion & "%"
                usu.Where.DescripcionRole.Operator = WhereParameter.Operand.Like
            End If

            usu.Query.AddOrderBy("DescripcionRole", WhereParameter.Dir.ASC)

            If usu.Query.Load Then
                If usu.DefaultView.Table.Rows.Count > 0 Then
                    Return usu.DefaultView.Table
                Else
                    Return Nothing
                End If

            Else
                Return Nothing

            End If

        Catch ex As Exception
            MessageError = ex
            Return Nothing
        End Try

    End Function

    Public Function limpiarTablas(conn As String, query As String, codUsuario As Integer) As Integer

        Try
            Dim usu As New Class_RC_Data.Class_RC.USUARIOS(conn)
            Insert_Log(conn, "Limpieza de registros o tablas, query: " & query, codUsuario, 40, Nothing)
            Return usu.excuteQueryDataBase(conn, query)

        Catch ex As Exception
            MessageError = ex
            Return 0

        End Try

    End Function

#End Region


#Region "TipoProductos"

    Public Function Insert_TipoProducto(conn As String, descTipoProducto As String) As Integer

        Dim tx As TransactionMgr
        tx = TransactionMgr.ThreadTransactionMgr()

        Try
            tx.BeginTransaction()

            Dim tipoProd As New Class_RC_Data.Class_RC.TIPOPRODUCTO(conn)

            tipoProd.AddNew()
            tipoProd.DescTipoProducto = descTipoProducto
            tipoProd.Save()

            tx.CommitTransaction()

            Return tipoProd.CodTipoProducto

        Catch ex As Exception
            tx.RollbackTransaction()
            MessageError = ex
            Return 0
        End Try

    End Function

    Public Function Update_TipoProducto(conn As String, codTipoProducto As Integer, descTipoProducto As String) As Boolean

        Dim tx As TransactionMgr
        tx = TransactionMgr.ThreadTransactionMgr()

        Try
            tx.BeginTransaction()

            Dim tipoProducto As New Class_RC_Data.Class_RC.TIPOPRODUCTO(conn)
            tipoProducto.Where.CodTipoProducto.Value = codTipoProducto

            If tipoProducto.Query.Load Then

                tipoProducto.DescTipoProducto = descTipoProducto
                tipoProducto.Save()

            End If

            tx.CommitTransaction()
            Return True

        Catch ex As Exception
            tx.RollbackTransaction()
            MessageError = ex
            Return False
        End Try

    End Function

    Public Function Delete_TipoProducto(conn As String, codTipoProducto As Integer) As Boolean

        Dim tx As TransactionMgr
        tx = TransactionMgr.ThreadTransactionMgr()

        Try
            tx.BeginTransaction()

            Dim tipoProducto As New Class_RC_Data.Class_RC.TIPOPRODUCTO(conn)
            tipoProducto.Where.CodTipoProducto.Value = codTipoProducto

            If tipoProducto.Query.Load Then

                tipoProducto.MarkAsDeleted()
                tipoProducto.Save()

            End If

            tx.CommitTransaction()
            Return True

        Catch ex As Exception
            tx.RollbackTransaction()
            MessageError = ex
            Return False
        End Try

    End Function

    Public Function get_TiposProducto_All(conn As String, codTipoProducto As Integer, descripcion As String) As DataTable
        Try

            Dim tipoProducto As New Class_RC_Data.Class_RC.TIPOPRODUCTO(conn)

            If codTipoProducto > 0 Then
                tipoProducto.Where.CodTipoProducto.Value = codTipoProducto
            End If

            If descripcion <> "" Then
                tipoProducto.Where.DescTipoProducto.Value = "%" & descripcion & "%"
                tipoProducto.Where.DescTipoProducto.Operator = WhereParameter.Operand.Like
            End If

            If tipoProducto.Query.Load Then
                If tipoProducto.DefaultView.Table.Rows.Count > 0 Then
                    Return tipoProducto.DefaultView.Table
                Else
                    Return Nothing
                End If

            Else
                Return Nothing

            End If

        Catch ex As Exception
            MessageError = ex
            Return Nothing
        End Try

    End Function

#End Region

#Region "TipoMaterial"

#End Region

#Region "Actividades"

    Public Function Insert_Actividad(ByVal conn As String, ByVal DescActividad As String, ByVal CodSector As Integer, ByVal ObsAct As String) As Integer

        Dim tx As TransactionMgr
        tx = TransactionMgr.ThreadTransactionMgr()

        Try
            tx.BeginTransaction()

            Dim Actividad As New Class_RC_Data.Class_RC.ACTIVIDADES(conn)

            Actividad.AddNew()
            Actividad.DescActividad = DescActividad
            Actividad.CodSector = CodSector
            Actividad.ObsAct = ObsAct
            Actividad.Save()

            tx.CommitTransaction()

            Return Actividad.CodActividad

        Catch ex As Exception
            tx.RollbackTransaction()
            MessageError = ex
            Return 0
        End Try

        '177630/-13

    End Function

    Public Function Update_Actividades(ByVal conn As String, ByVal CodActividad As Integer, ByVal DescActividad As String, ByVal CodSector As Integer, ByVal ObsAct As String) As Boolean

        Dim tx As TransactionMgr
        tx = TransactionMgr.ThreadTransactionMgr()

        Try
            tx.BeginTransaction()

            Dim Actividad As New Class_RC_Data.Class_RC.ACTIVIDADES(conn)
            Actividad.Where.CodActividad.Value = CodActividad

            If Actividad.Query.Load Then

                Actividad.DescActividad = DescActividad
                Actividad.CodSector = CodSector
                Actividad.ObsAct = ObsAct
                Actividad.Save()

            End If

            tx.CommitTransaction()
            Return True

        Catch ex As Exception
            tx.RollbackTransaction()
            MessageError = ex
            Return False
        End Try

    End Function

    Public Function Delete_Actividad(ByVal conn As String, ByVal CodActividad As Integer) As Boolean

        Dim tx As TransactionMgr
        tx = TransactionMgr.ThreadTransactionMgr()

        Try
            tx.BeginTransaction()

            Dim Actividad As New Class_RC_Data.Class_RC.ACTIVIDADES(conn)
            Actividad.Where.CodActividad.Value = CodActividad

            If Actividad.Query.Load Then

                Actividad.MarkAsDeleted()
                Actividad.Save()

            End If

            tx.CommitTransaction()
            Return True

        Catch ex As Exception
            tx.RollbackTransaction()
            MessageError = ex
            Return False
        End Try

    End Function

    Public Function get_Actividad_All(ByVal conn As String, ByVal CodActividad As Integer, ByVal DescActividad As String, ByVal CodSector As Integer) As DataTable
        Try

            Dim vActividad As New Class_RC_Data.Class_RC.VistaActividades(conn)

            If CodActividad > 0 Then
                vActividad.Where.CodActividad.Value = CodActividad
            End If

            If DescActividad <> "" Then
                vActividad.Where.DescActividad.Value = "%" & DescActividad & "%"
                vActividad.Where.DescActividad.Operator = WhereParameter.Operand.Like
            End If
            If CodSector > 0 Then
                vActividad.Where.CodSector.Value = CodSector
            End If

            If vActividad.Query.Load Then
                If vActividad.DefaultView.Table.Rows.Count > 0 Then
                    Return vActividad.DefaultView.Table
                Else
                    Return Nothing
                End If

            Else
                Return Nothing

            End If

        Catch ex As Exception
            MessageError = ex
            Return Nothing
        End Try

    End Function

    Public Function get_Sector(ByVal conn As String) As DataTable
        Try

            Dim vSector As New Class_RC_Data.Class_RC.SECTORES(conn)

            If vSector.Query.Load Then
                If vSector.DefaultView.Table.Rows.Count > 0 Then
                    Return vSector.DefaultView.Table
                Else
                    Return Nothing
                End If

            Else
                Return Nothing

            End If

        Catch ex As Exception
            MessageError = ex
            Return Nothing
        End Try

    End Function
#End Region

#Region "Areas"

    Public Function Insert_Areas(ByVal conn As String, ByVal descArea As String, ByVal ObsArea As String) As Integer

        Dim tx As TransactionMgr
        tx = TransactionMgr.ThreadTransactionMgr()

        Try
            tx.BeginTransaction()

            Dim Area As New Class_RC_Data.Class_RC.AREAS(conn)

            Area.AddNew()
            Area.DescArea = descArea
            Area.ObsArea = ObsArea
            Area.Save()

            tx.CommitTransaction()

            Return Area.CodArea

        Catch ex As Exception
            tx.RollbackTransaction()
            MessageError = ex
            Return 0
        End Try

        '177630/-13

    End Function

    Public Function Update_Area(ByVal conn As String, ByVal CodArea As Integer, ByVal desArea As String, ByVal ObsArea As String) As Boolean

        Dim tx As TransactionMgr
        tx = TransactionMgr.ThreadTransactionMgr()

        Try
            tx.BeginTransaction()

            Dim Area As New Class_RC_Data.Class_RC.AREAS(conn)
            Area.Where.CodArea.Value = CodArea

            If Area.Query.Load Then

                Area.DescArea = desArea
                Area.ObsArea = ObsArea
                Area.Save()

            End If

            tx.CommitTransaction()
            Return True

        Catch ex As Exception
            tx.RollbackTransaction()
            MessageError = ex
            Return False
        End Try

    End Function

    Public Function Delete_Area(ByVal conn As String, ByVal codarea As Integer) As Boolean

        Dim tx As TransactionMgr
        tx = TransactionMgr.ThreadTransactionMgr()

        Try
            tx.BeginTransaction()

            Dim Area As New Class_RC_Data.Class_RC.AREAS(conn)
            Area.Where.CodArea.Value = codarea

            If Area.Query.Load Then

                Area.MarkAsDeleted()
                Area.Save()

            End If

            tx.CommitTransaction()
            Return True

        Catch ex As Exception
            tx.RollbackTransaction()
            MessageError = ex
            Return False
        End Try

    End Function

    Public Function get_Area_All(ByVal conn As String, ByVal codArea As Integer, ByVal descArea As String) As DataTable
        Try

            Dim vArea As New Class_RC_Data.Class_RC.AREAS(conn)

            If codArea > 0 Then
                vArea.Where.CodArea.Value = codArea
            End If

            If descArea <> "" Then
                vArea.Where.DescArea.Value = "%" & descArea & "%"
                vArea.Where.DescArea.Operator = WhereParameter.Operand.Like
            End If

            If vArea.Query.Load Then
                If vArea.DefaultView.Table.Rows.Count > 0 Then
                    Return vArea.DefaultView.Table
                Else
                    Return Nothing
                End If

            Else
                Return Nothing

            End If

        Catch ex As Exception
            MessageError = ex
            Return Nothing
        End Try

    End Function

#End Region

#Region "Sector"
    Public Function Insert_Sector(ByVal conn As String, ByVal descsector As String, ByVal codarea As Integer,
                                  ByVal obscsector As String) As Integer

        Dim tx As TransactionMgr
        tx = TransactionMgr.ThreadTransactionMgr()

        Try
            tx.BeginTransaction()

            Dim Sector As New Class_RC_Data.Class_RC.SECTORES(conn)

            Sector.AddNew()
            Sector.DescSector = descsector
            Sector.ObsSector = obscsector
            Sector.CodArea = codarea
            Sector.ObsSector = obscsector
            Sector.Save()

            tx.CommitTransaction()

            Return Sector.CodArea

        Catch ex As Exception
            tx.RollbackTransaction()
            MessageError = ex
            Return 0
        End Try
    End Function

    Public Function Update_Sector(ByVal conn As String, ByVal codsector As Integer, ByVal descsector As String, ByVal codarea As Integer,
                                  ByVal obscsector As String) As Boolean

        Dim tx As TransactionMgr
        tx = TransactionMgr.ThreadTransactionMgr()

        Try
            tx.BeginTransaction()

            Dim Sector As New Class_RC_Data.Class_RC.SECTORES(conn)
            Sector.Where.CodSector.Value = codsector

            If Sector.Query.Load Then

                Sector.DescSector = descsector
                Sector.CodArea = codarea
                Sector.ObsSector = obscsector
                Sector.Save()

            End If

            tx.CommitTransaction()
            Return True

        Catch ex As Exception
            tx.RollbackTransaction()
            MessageError = ex
            Return False
        End Try

    End Function

    Public Function Delete_Sector(ByVal conn As String, ByVal codsector As Integer) As Boolean

        Dim tx As TransactionMgr
        tx = TransactionMgr.ThreadTransactionMgr()

        Try
            tx.BeginTransaction()

            Dim Sector As New Class_RC_Data.Class_RC.SECTORES(conn)
            Sector.Where.CodArea.Value = codsector

            If Sector.Query.Load Then

                Sector.MarkAsDeleted()
                Sector.Save()

            End If

            tx.CommitTransaction()
            Return True

        Catch ex As Exception
            tx.RollbackTransaction()
            MessageError = ex
            Return False
        End Try

    End Function

    Public Function get_sector_All(ByVal conn As String, ByVal codsector As Integer, ByVal descsector As String, ByVal codarea As Integer) As DataTable
        Try

            Dim vSector As New Class_RC_Data.Class_RC.VistaSectores(conn)

            If codsector > 0 Then
                vSector.Where.CodSector.Value = codsector
            End If

            If descsector <> "" Then
                vSector.Where.DescSector.Value = "%" & descsector & "%"
                vSector.Where.DescSector.Operator = WhereParameter.Operand.Like
            End If
            If codarea > 0 Then
                vSector.Where.CodArea.Value = codarea
            End If
            If vSector.Query.Load Then
                If vSector.DefaultView.Table.Rows.Count > 0 Then
                    Return vSector.DefaultView.Table
                Else
                    Return Nothing
                End If

            Else
                Return Nothing

            End If

        Catch ex As Exception
            MessageError = ex
            Return Nothing
        End Try

    End Function


#End Region

#Region "Ciudad"
    Public Function Insert_Ciudad(ByVal conn As String, ByVal descripcion As String) As Integer

        Dim tx As TransactionMgr
        tx = TransactionMgr.ThreadTransactionMgr()

        Try
            tx.BeginTransaction()

            Dim ciudad As New Class_RC_Data.Class_RC.CIUDAD(conn)

            ciudad.AddNew()
            ciudad.Descripcion = descripcion

            ciudad.Save()

            tx.CommitTransaction()

            Return ciudad.Codciudad

        Catch ex As Exception
            tx.RollbackTransaction()
            MessageError = ex
            Return 0
        End Try
    End Function

    Public Function Update_Ciudad(ByVal conn As String, ByVal codciudad As Integer, ByVal descripcion As String) As Boolean

        Dim tx As TransactionMgr
        tx = TransactionMgr.ThreadTransactionMgr()

        Try
            tx.BeginTransaction()

            Dim ciudad As New Class_RC_Data.Class_RC.CIUDAD(conn)
            ciudad.Where.Codciudad.Value = codciudad

            If ciudad.Query.Load Then

                ciudad.Descripcion = descripcion

                ciudad.Save()

            End If

            tx.CommitTransaction()
            Return True

        Catch ex As Exception
            tx.RollbackTransaction()
            MessageError = ex
            Return False
        End Try

    End Function

    Public Function Delete_Ciudad(ByVal conn As String, ByVal codciudad As Integer) As Boolean

        Dim tx As TransactionMgr
        tx = TransactionMgr.ThreadTransactionMgr()

        Try
            tx.BeginTransaction()

            Dim ciudad As New Class_RC_Data.Class_RC.CIUDAD(conn)
            ciudad.Where.Codciudad.Value = codciudad

            If ciudad.Query.Load Then

                ciudad.MarkAsDeleted()
                ciudad.Save()

            End If

            tx.CommitTransaction()
            Return True

        Catch ex As Exception
            tx.RollbackTransaction()
            MessageError = ex
            Return False
        End Try

    End Function

    Public Function get_Ciudad_All(ByVal conn As String, ByVal codciudad As Integer, ByVal descripcion As String) As DataTable
        Try

            Dim vciudad As New Class_RC_Data.Class_RC.CIUDAD(conn)

            If codciudad > 0 Then
                vciudad.Where.Codciudad.Value = codciudad
            End If

            If descripcion <> "" Then
                vciudad.Where.Descripcion.Value = "%" & descripcion & "%"
                vciudad.Where.Descripcion.Operator = WhereParameter.Operand.Like
            End If

            If vciudad.Query.Load Then
                If vciudad.DefaultView.Table.Rows.Count > 0 Then
                    Return vciudad.DefaultView.Table
                Else
                    Return Nothing
                End If

            Else
                Return Nothing

            End If

        Catch ex As Exception
            MessageError = ex
            Return Nothing
        End Try

    End Function

#End Region

#Region "Tipo Material"
    Public Function Insert_TipoMaterial(ByVal conn As String, ByVal descTipoMaterial As String, ByVal codUnidadMedida As Integer) As Integer

        Dim tx As TransactionMgr
        tx = TransactionMgr.ThreadTransactionMgr()

        Try
            tx.BeginTransaction()

            Dim tipomaterial As New Class_RC_Data.Class_RC.TIPOMATERIAL(conn)

            tipomaterial.AddNew()
            tipomaterial.DescTipoMaterial = descTipoMaterial
            tipomaterial.CodUnidadMedida = codUnidadMedida
            tipomaterial.Save()
            tx.CommitTransaction()

            Return tipomaterial.CodTipoMaterial

        Catch ex As Exception
            tx.RollbackTransaction()
            MessageError = ex
            Return 0
        End Try
    End Function

    Public Function Update_TipoMaterial(ByVal conn As String, ByVal codTipoMaterial As Integer, ByVal descTipoMaterial As String, ByVal codUniMendida As Integer) As Boolean

        Dim tx As TransactionMgr
        tx = TransactionMgr.ThreadTransactionMgr()

        Try
            tx.BeginTransaction()

            Dim tipomaterial As New Class_RC_Data.Class_RC.TIPOMATERIAL(conn)
            tipomaterial.Where.CodTipoMaterial.Value = codTipoMaterial

            If tipomaterial.Query.Load Then

                tipomaterial.DescTipoMaterial = descTipoMaterial
                tipomaterial.CodUnidadMedida = codUniMendida

                tipomaterial.Save()

            End If

            tx.CommitTransaction()
            Return True

        Catch ex As Exception
            tx.RollbackTransaction()
            MessageError = ex
            Return False
        End Try

    End Function

    Public Function Delete_TipoMaterial(ByVal conn As String, ByVal codTipoMaterial As Integer) As Boolean

        Dim tx As TransactionMgr
        tx = TransactionMgr.ThreadTransactionMgr()

        Try
            tx.BeginTransaction()

            Dim tipomaterial As New Class_RC_Data.Class_RC.TIPOMATERIAL(conn)
            tipomaterial.Where.CodTipoMaterial.Value = codTipoMaterial

            If tipomaterial.Query.Load Then

                tipomaterial.MarkAsDeleted()
                tipomaterial.Save()

            End If

            tx.CommitTransaction()
            Return True

        Catch ex As Exception
            tx.RollbackTransaction()
            MessageError = ex
            Return False
        End Try

    End Function

    Public Function get_TipoMaterial_All(ByVal conn As String, ByVal codTipoMaterial As Integer, ByVal descTipoMaterial As String, ByVal codUniMedida As Integer) As DataTable
        Try

            Dim vTipoMaterial As New Class_RC_Data.Class_RC.VistaTipoMaterial(conn)

            If codTipoMaterial > 0 Then
                vTipoMaterial.Where.CodTipoMaterial.Value = codTipoMaterial
            End If

            If descTipoMaterial <> "" Then
                vTipoMaterial.Where.DescTipoMaterial.Value = "%" & descTipoMaterial & "%"
                vTipoMaterial.Where.DescTipoMaterial.Operator = WhereParameter.Operand.Like
            End If
            If codUniMedida > 0 Then
                vTipoMaterial.Where.CodUnidadMedida.Value = codUniMedida
            End If
            If vTipoMaterial.Query.Load Then
                If vTipoMaterial.DefaultView.Table.Rows.Count > 0 Then
                    Return vTipoMaterial.DefaultView.Table
                Else
                    Return Nothing
                End If

            Else
                Return Nothing

            End If

        Catch ex As Exception
            MessageError = ex
            Return Nothing
        End Try

    End Function
    Public Function get_tipo_unidMedida(ByVal conn As String) As DataTable
        Try

            Dim vTipoUni As New Class_RC_Data.Class_RC.UNIMEDIDA(conn)

            If vTipoUni.Query.Load Then
                If vTipoUni.DefaultView.Table.Rows.Count > 0 Then
                    Return vTipoUni.DefaultView.Table
                Else
                    Return Nothing
                End If

            Else
                Return Nothing

            End If

        Catch ex As Exception
            MessageError = ex
            Return Nothing
        End Try

    End Function
#End Region

End Class
